<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <!-- <el-form-item label="添加方式" prop="title">
        <el-radio-group v-model="addType" class="ml-4">
          <el-radio value="1" size="large" checked>添加</el-radio>
          <el-radio value="2" size="large">匹配</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item label="所属组织" prop="CompanyID">
        <el-cascader
          v-model="form.CompanyID"
          :options="companyoptions"
          :props="companyProps"
          clearable
          @change="getDeptment"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="DepartmentID">
        <el-cascader
          v-model="form.DepartmentID"
          :options="DeptmentOptions"
          :props="deptmentProps"
          clearable
          @change="getStaffPost"
        />
      </el-form-item>
      <el-form-item label="职级" prop="StaffPostRoleID">
        <el-select
          v-model="form.StaffPostRoleID"
          style="width: 150px"
          @change="getStaffPost"
        >
          <el-option
            v-for="item in staffpostroleoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职位" prop="StaffPostID">
        <el-cascader
          v-model="form.StaffPostID"
          :options="StaffPostOptions"
          :props="StaffPostProps"
          clearable
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="StaffName">
        <el-input v-model="form.StaffName" />
      </el-form-item>
      <el-form-item label="手机号" prop="Mobile">
        <el-input v-model="form.Mobile" type="number" />
      </el-form-item>
      <el-form-item label="工号" prop="LoginName">
        <el-input v-model="form.LoginName" />
      </el-form-item>
      <!-- <el-form-item label="密码设置" prop="LoginPassword">
        <el-input v-model="form.LoginPassword" />
      </el-form-item> -->
      <el-form-item label="状态" prop="StaffState">
        <el-select v-model="form.StaffState" style="width: 150px">
          <el-option
            v-for="item in StaffStateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { getDeptmentSelect, getStaffPostSelect } from '@/api/common'
  import { saveStaff } from '@/api/systemsetOrganizationStaff'

  export default defineComponent({
    name: 'SystemsetOrganizationStaffEdit',
    props: {
      companyoptions: {
        type: Array,
        default: () => [],
      },
      staffpostroleoptions: {
        type: Array,
        default: () => [],
      },
    },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {
          StaffID: 0,
          StaffPostRole: 8,
          StaffState: 1,
        },
        rules: {
          StaffName: [
            { required: true, trigger: 'blur', message: '请输入员工姓名' },
          ],
          Mobile: [
            {
              validator: () => {
                if (!state.form.Mobile) {
                  return Promise.reject('手机号不能为空')
                } else if (state.form.Mobile.length !== 11) {
                  return Promise.reject('手机不符合标准11位')
                }
                // else if (!isValidPhone()) {
                //   return Promise.reject('手机号格式不正确')
                // }
                return Promise.resolve()
              },
              required: true,
              trigger: 'blur',
            },
          ],
          LoginName: [
            {
              validator: () => {
                if (!state.form.LoginName) {
                  return Promise.reject('登录账号不能为空')
                } else if (state.form.LoginName.length !== 12) {
                  return Promise.reject('登录账号不符合标准12位')
                }
                // else if (state.form.LoginName.substring(0, 6) !== 'sanjin') {
                //   return Promise.reject('登录账号不符合标准sanjin+6位数字')
                // }
                return Promise.resolve()
              },
              required: true,
              trigger: 'blur',
            },
          ],
          LoginPassword: [
            {
              validator: () => {
                if (!state.form.LoginPassword) {
                  return Promise.reject('请输入登录密码')
                } else if (state.form.LoginPassword.length < 6) {
                  return Promise.reject('登录密码不能少于6位')
                }
                return Promise.resolve()
              },
              required: true,
              trigger: 'blur',
            },
          ],
        },
        addType: 1,
        title: '',
        dialogFormVisible: false,
        companyProps: {
          multiple: true,
          checkStrictly: true,
          expandTrigger: 'hover',
        },
        deptmentProps: {
          multiple: true,
          checkStrictly: true,
          expandTrigger: 'hover',
        },
        StaffPostProps: {
          multiple: true,
          expandTrigger: 'hover',
        },
        // CompanyOptions: props.companyoptions,
        DeptmentOptions: [],
        // StaffPostRoleOptions: props.staffpostroleoptions,
        StaffPostOptions: [],
        StaffStateOptions: [
          { label: '在职', value: 1 },
          { label: '暂停账号使用', value: -1 },
          { label: '已离职', value: -2 },
          // { label: '未设定', value: 0 },
        ],
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
          getDeptment()
          getStaffPost()
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          StaffID: 0,
          StaffPostRole: 8,
          StaffState: 1,
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveStaff(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const isValidPhone = () => {
        const regex = /^1[3-9]\d{9}$/ // 中国大陆手机号正则，以1开头，第二位是3-9，后面是9位数字
        return regex.test(state.form.Mobile)
        // console.log('state.form.Mobile', regex.test(state.form.Mobile))
      }

      const getDeptment = async () => {
        console.log('form.Company', state.form.CompanyID)
        const getParams = {
          Company: state.form.CompanyID,
        }
        const {
          data: { DeptmentOptions },
        } = await getDeptmentSelect(getParams)
        state.DeptmentOptions = DeptmentOptions
      }

      const getStaffPost = async () => {
        console.log('form.Company', state.form.CompanyID)
        const getParams = {
          Company: state.form.CompanyID,
          Deptment: state.form.DepartmentID,
          StaffPostRole: state.form.StaffPostRoleID,
        }
        const {
          data: { StaffPostOptions },
        } = await getStaffPostSelect(getParams)
        state.StaffPostOptions = StaffPostOptions
      }

      // const getOptions = async () => {
      //   state.listLoading = true
      //   const {
      //     data: { CompanyOptions },
      //   } = await getCompanySelect()
      //   const {
      //     data: { StaffPostRoleOptions },
      //   } = await getStaffPostRoleSelect()
      //   state.CompanyOptions = CompanyOptions
      //   state.StaffPostRoleOptions = StaffPostRoleOptions
      //   state.listLoading = false
      // }

      // onMounted(() => {
      //   getOptions()
      // })

      return {
        ...toRefs(state),
        showEdit,
        getDeptment,
        getStaffPost,
        isValidPhone,
        close,
        save,
      }
    },
  })
</script>
